<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            position: absolute;
             width: 600px;
             height: 100px;
            left: 50%;
            top: 50%;
        }

       .box div {

            float: left;
            width: 100px;
            height: 100px;
            background-color: cyan;
            margin-right:10px;
            color: white;
            font-weight: 1000;
            text-align: center;
            line-height: 100px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>移动的盒子</div>
        <div>移动的盒子</div>
        <div>移动的盒子</div>
        <div>移动的盒子</div>
    </div>
    <script>
        var div = document.querySelector('.box')
        document.addEventListener("keydown", function (e) {
            var sh = div.offsetTop
            var zuo = div.offsetLeft
            if (e.keyCode === 38) {
                sh -= 20
                div.style.top = sh + 'px'
            }

            if (e.keyCode === 37) {
                zuo -= 20
                div.style.left = zuo + 'px'
            }


            if (e.keyCode === 40) {
                sh += 20;
                div.style.top = sh + 'px'
            }
            if (e.keyCode === 39) {
                zuo += 20
                div.style.left = zuo + 'px'
            }
        })
    </script>

</body>

</html>